package net.lanelife.casino.caipiao.job.handler;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.lanelife.casino.caipiao.entity.BankTransactionDetail;
import net.lanelife.casino.caipiao.entity.Job;
import net.lanelife.casino.caipiao.entity.User;
import net.lanelife.casino.caipiao.entity.WithdrawRecord;
import net.lanelife.casino.caipiao.job.info.WithdrawAcceptJobInfo;
import net.lanelife.casino.caipiao.service.api.IWithdrawRecordService;
import net.lanelife.casino.common.utils.StringUtils;

import org.joda.time.DateTime;
import org.joda.time.LocalDate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

/**
 * 提款通过任务处理器
 * @author Lane
 *
 */
@Component
public class WithdrawAcceptJobHandler extends TransactionJobHandler {
	
	@Autowired
	private IWithdrawRecordService withdrawRecordService;
	
	@Transactional(rollbackFor = Exception.class)
	public void handle(Job job) throws Exception {
		
		final WithdrawAcceptJobInfo jobInfo = job.readInfo(WithdrawAcceptJobInfo.class);               // 任务信息
		final WithdrawRecord withdrawRecord = withdrawRecordService.find(jobInfo.getRID());            // 提款记录
		final User                     user = withdrawRecord.getUser();                                // 提款用户
		final LocalDate          reportDate = new LocalDate(job.getCreatedDate().getMillis());         // 报表日期
		final Double                 amount = withdrawRecord.getAmount();                              // 提款金额
		final Double                    fee = withdrawRecord.getFee();                                 // 平台手续费
		final Double                bankFee = withdrawRecord.getBankFee();                             // 银行手续费
		Map<String, Object>      properties = new HashMap<String, Object>();                           // 更新字段
		
		properties.put("WITHDRAW_TIMES_ACCEPT", 1);
		properties.put("WITHDRAW_TOTAL_ACCEPT", amount);
		properties.put("WITHDRAW_FEE_ACCEPT", fee);
		properties.put("WITHDRAW_BANK_FEE_ACCEPT", bankFee);
		
		saveBankTransactionDetail(
				BankTransactionDetail.TransactionDirection.OUT, 
				BankTransactionDetail.SourceUsesType.CK, 
				new DateTime(),//jobInfo.getTIME()
				withdrawRecord.getPayerBankName(), 
				withdrawRecord.getPayerAccountName(), 
				withdrawRecord.getPayerAccountNo(), 
				withdrawRecord.getPayeeBankName(), 
				withdrawRecord.getPayeeAccountName(), 
				withdrawRecord.getPayeeAccountNo(), 
				amount - fee, 
				bankFee, 
				jobInfo.getBCB(), 
				user.getUserName()
		);
		
		// 更新用户统计表
		updateUserStatistics(user, properties);
		List<User> teamUsers = userService.findAll(StringUtils.toIntegerList(user.getPath(), "\\|"));
		for (User tuser : teamUsers) {
			// 更新团队统计表
			updateTeamStatistics(tuser, properties);
			// 更新团队日结表
			updateTeamDailyStatistics(tuser, reportDate, properties);
		}
		// 更新公司统计表
		updateCompanyStatistics(properties);
		// 更新公司日结表
		updateCompanyDailyStatistics(reportDate, properties);
		
	}

}
